// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NKactive 
// Original source by kyle algo v1
//@version=5
// 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

strategy("NK kyle algo v1", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, pyramiding=0, slippage=1)
//indicator("NK kyle algo v1", overlay=true, precision=0, explicit_plot_zorder=true, max_labels_count=500)

import EliCobra/CobraMetrics/4 as cobra
//// PLOT DATA
disp_ind = input.string ("None" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
pos_table = input.string("Middle Left", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
type_table = input.string("None", "Table Type", options = ["Full", "Simple", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
plot(cobra.curve(disp_ind))
cobra.cobraTable(type_table, pos_table)
//
// ****************************************************************************************************************************************************************
//  algo
// ****************************************************************************************************************************************************************

// Inputs 
timeframeAlgo=input.timeframe(defval ='1D', group = "Algo", tooltip = "Select a different timeframe for this series") // Use Alternative timeframe
sensitivity = input.float(2.8, "  Sensitivity (0.5 - 10)", 0.5, 10, step=0.1, group = "Algo")
emaEnergy   = input.bool(true, "EMA Energy", group = "Algo")
keltner_length = input(10, "Keltner Channel Length", group = "Algo")
atrPeriod = input(10, "ATR Length", group = "Algo")
factor = input.float(3.5, "Factor", step = 0.01, group = "Algo")

// =request.security(syminfo.tickerid,timeframe, high)

// Keltner Channel function
keltner_channel(src, length) =>
    ma = request.security(syminfo.tickerid,timeframeAlgo,ta.sma(src, length))
    rangec = request.security(syminfo.tickerid,timeframeAlgo, high) - request.security(syminfo.tickerid,timeframeAlgo, low)
    upper = ma + rangec
    lower = ma - rangec
    [upper, lower]

// Modified Supertrend function using Keltner Channel
supertrend(_src, factor, atrLen, kel_length) =>
    [upperKeltner, lowerKeltner] = keltner_channel(_src, kel_length)
    rangec = upperKeltner - lowerKeltner
    upperBand = _src + factor * rangec
    lowerBand = _src - factor * rangec
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])
    lowerBand := lowerBand > prevLowerBand or request.security(syminfo.tickerid,timeframeAlgo,close[1]) < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or request.security(syminfo.tickerid,timeframeAlgo,close[1]) > prevUpperBand ? upperBand : prevUpperBand
    int direction = na
    float superTrend = na
    prevSuperTrend = superTrend[1]

    if na(rangec[1])
        direction := 1
    else if prevSuperTrend == prevUpperBand
        direction := close > upperBand ? -1 : 1
    else
        direction := close < lowerBand ? 1 : -1
    superTrend := direction == -1 ? lowerBand : upperBand
    [superTrend, direction]
// Get Components
ema1        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high,  9))
ema2        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 12))
ema3        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 15))
ema4        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 18))
ema5        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 21))
ema6        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 24))
ema7        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 27))
ema8        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 30))
ema9        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 33))
ema10        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 36))
ema11        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 39))
ema12       = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 42))
ema13       = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 45))
ema14        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 48))
ema15        = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(high, 51))
// Colors
green       = #2BBC4D, red     = #C51D0B
emaEnergyColor(ma) => emaEnergy ? (close >= ma ? green : red) : na
// Plots
plot(ema1, "", emaEnergyColor(ema1), editable=false)
plot(ema2, "", emaEnergyColor(ema2), editable=false)
plot(ema3, "", emaEnergyColor(ema3), editable=false)
plot(ema4, "", emaEnergyColor(ema4), editable=false)
plot(ema5, "", emaEnergyColor(ema5), editable=false)
plot(ema6, "", emaEnergyColor(ema6), editable=false)
plot(ema7, "", emaEnergyColor(ema7), editable=false)
plot(ema8, "", emaEnergyColor(ema8), editable=false)
plot(ema9, "", emaEnergyColor(ema9), editable=false)
plot(ema10, "", emaEnergyColor(ema10), editable=false)
plot(ema11, "", emaEnergyColor(ema11), editable=false)
plot(ema12, "", emaEnergyColor(ema12), editable=false)
plot(ema13, "", emaEnergyColor(ema13), editable=false)
plot(ema14, "", emaEnergyColor(ema14), editable=false)
plot(ema15, "", emaEnergyColor(ema15), editable=false)
[supertrend, direction] = request.security(syminfo.tickerid,timeframeAlgo,supertrend(close, sensitivity, 11, keltner_length))
bull = request.security(syminfo.tickerid,timeframeAlgo,ta.crossover(close, supertrend))
bear = request.security(syminfo.tickerid,timeframeAlgo,ta.crossunder(close, supertrend))
y1 = request.security(syminfo.tickerid,timeframeAlgo,low) - (ta.atr(30) * 2)
y2 = request.security(syminfo.tickerid,timeframeAlgo,high) + (ta.atr(30) * 2)
buy  = bull ? label.new(bar_index, y1, "BUY", xloc.bar_index, yloc.price, green, label.style_label_up, color.white, size.normal) : na
sell = bear ? label.new(bar_index, y2, "SELL", xloc.bar_index, yloc.price, red, label.style_label_down, color.white, size.normal) : na
[supertrends, directions] = ta.supertrend(factor, atrPeriod)
bodyMiddle = plot((open + close) / 2, display=display.none)
// Trend Catcher Indicator (Example)
ema100 = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(close, 10))
ema200 = request.security(syminfo.tickerid,timeframeAlgo,ta.ema(close, 20))
trendCatcher = request.security(syminfo.tickerid,timeframeAlgo,ta.crossover(ema100, ema200)) ? 1 : request.security(syminfo.tickerid,timeframeAlgo,ta.crossunder(ema100, ema200)) ? -1 : 0
trendColor = trendCatcher == 1 ? color.rgb(90, 23, 102) : na
barcolor(trendColor)
// Colored candles
barcolor(color = close > supertrends ? color.rgb(102, 255, 0) : color.rgb(255, 0, 0))
//alertcondition(bull, title="Buy", message="Buy!")
//alertcondition(bear, title="Sell", message="Sell!")






// ****************************************************************************************************************************************************************
// Call combine signals and execute buy/sell positions within timeframe
//.****************************************************************************************************************************************************************
// Date Range To Include
startDate = timestamp("2018-01-01T00:00")
endDate = time
// Check if the current timestamp is within the restricted range
inRestrictedRange = time >= startDate and time <= endDate
//
// Buy Signals on overbought and oversold
//
if inRestrictedRange and bull // ADD OTHER BUY SIGNAL BOOLS
    strategy.entry("My Long Entry Id", strategy.long, 100)
if inRestrictedRange and bear // ADD OTHER BUY SIGNAL BOOLS
    strategy.entry("My Short Entry Id", strategy.short, 100)